<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>病人登录</title>
    <script src="{{url_for('static', filename='js/jquery-3.6.0.min.js')}}"></script>
    <script src="{{url_for('static', filename='js/custom_message_box.js')}}"></script>
    <link rel="stylesheet" href="{{url_for('static', filename='css/custom_message_box.css')}}">
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 20px;
            padding: 0;
            background: url("{{url_for('static', filename='img/login_admin_web.jpg')}}") no-repeat center;
            background-size: cover;
        }

        h2 {
            margin-bottom: 20px;
        }

        form {
            max-width: 300px;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            box-sizing: border-box;
            font-size: 16px;
        }

        button[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }

        button[type="submit"]:hover {
            background-color: #45a049;
        }

        a {
            display: block;
            margin-top: 10px;
            text-decoration: none;
            color: #1e90ff;
            font-size: 14px;
        }

        a:hover {
            text-decoration: underline;
        }

        #login_box {
            background-color: rgba(226, 234, 244, 0.5);
            height: 300px;
            width: 250px;
            margin: 250px auto;
            border-radius: 20px;
            padding: 50px;
        }
    </style>
    <script>
        $(document).ready(function () {
            $('#loginForm').submit(function (event) {
                event.preventDefault();
                let username = $('#username').val().trim();
                let password = $('#password').val().trim();

                // Simple form validation
                if (username === '' || password === '') {
                    showMessage("error", "请输入用户名和密码。", "message", 3000);
                    return;
                }

                let formData = {
                    username: username,
                    password: password
                };

                $.ajax({
                    type: 'POST',
                    url: "{{ url_for('patient.login_patient') }}",
                    data: JSON.stringify(formData),
                    contentType: 'application/json',
                    success: function (response) {
                        if (response.code === 0) {
                            // Login successful, redirect to patient dashboard or other page
                            window.location.replace("{{ url_for('patient.dashboard_patient_web') }}");
                        } else {
                            // Login failed, show error message
                            showMessage("error", response.message, "message", 3000);
                        }
                    },
                    error: function () {
                        showMessage("error", "网络错误，请稍后再试。", "message", 3000);
                    }
                });
            });

        });
    </script>
</head>

<body>
    <div id="message"></div>
    <div id="login_box">
        <h2 style="text-align: center">病人登录</h2>
        <form id="loginForm">
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" placeholder="用户名"><br>
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" placeholder="密码"><br>
            <button type="submit">登录</button>
        </form>
        <a href="{{ url_for('patient.register_patient_web') }}">注册</a>

    </div>
</body>

</html>